﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace newTurtle8
{
   
    public partial class TahasukaNewPanel : UserControl
    {
        private TahasukaCard tCard;

        public TahasukaNewPanel(TahasukaCard tCard)
        {
            InitializeComponent();
            this.tCard = tCard;
            this.Dock = DockStyle.Fill;

            this.dateTimePickerStart.Value = DateTime.Today;
            this.dateTimePickerResponsibility.Value = this.dateTimePickerStart.Value.AddDays(3);
            this.dateTimePickerSiurKav.Value = this.dateTimePickerStart.Value.AddMonths(-1);
            this.dateTimePickerEnd.Value = this.dateTimePickerStart.Value.AddDays(23);
        }

        private bool checkDates()
        {
            if (  (dateTimePickerStart.Value.CompareTo(dateTimePickerEnd.Value) > 0 )            ||
                  (dateTimePickerResponsibility.Value.CompareTo(dateTimePickerStart.Value) < 0)  ||
                  (dateTimePickerResponsibility.Value.CompareTo(dateTimePickerEnd.Value) > 0)    ||
                  (dateTimePickerSiurKav.Value.CompareTo(dateTimePickerEnd.Value) > 0 )
               )
            {
                MessageBox.Show("בחר תאריכים הגיוניים", "שגיאות בפרטים", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return false;
            }
            return true;
        }

        private bool PaintWithYellow()
        {
            bool yellow = false;

            if (textBoxLocation.Text.Equals(string.Empty))
            {
                textBoxLocation.BackColor = Color.Yellow;
                yellow = true;
            }

            bool ok = false;

            foreach (string item in comboBoxRegion.Items)
            {
                if (item.CompareTo(comboBoxRegion.Text) == 0)
                {
                    ok = true;
                    break;
                }
            }

            if ((comboBoxRegion.Text.Equals(string.Empty)) || (!ok))
            {
                comboBoxRegion.BackColor = Color.Yellow;
                yellow = true;
            }

            return yellow;
        }

        /// <summary>
        /// validates location and date values.
        /// INSERT's a row inro TahasukaData table.
        /// CREATE's a new Zimunim table.
        /// populates just created table with all active soldiers.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
    
        private void buttonGo_Click(object sender, EventArgs e)
        {
            textBoxLocation.BackColor = Color.White;
            comboBoxRegion.BackColor = Color.White;

           //this case occurs when the id is valid --->>> no current open Tahasuka --->>> create new one.
            if (validateUserInput())
            {
                SqlCommand sqlCmd = SqlManager.SqlCommandFactory(SqlManager.getSqlStringFromFile(@"\INSERT\TahasukaNewPanel.INSERT.sql"));

                sqlCmd.Parameters.Add("@location", SqlDbType.NVarChar).Value = textBoxLocation.Text;
                sqlCmd.Parameters.Add("@startDate", SqlDbType.Date).Value = dateTimePickerStart.Value;
                sqlCmd.Parameters.Add("@endDate", SqlDbType.Date).Value = dateTimePickerEnd.Value;
                sqlCmd.Parameters.Add("@region", SqlDbType.NVarChar).Value = comboBoxRegion.Text;
                sqlCmd.Parameters.Add("@siurKavDate", SqlDbType.Date).Value = dateTimePickerSiurKav.Value;
                sqlCmd.Parameters.Add("@responsibilityDate", SqlDbType.Date).Value = dateTimePickerResponsibility.Value;
                sqlCmd.Parameters.Add("@isClosed", SqlDbType.Bit).Value = false;

                tCard.createNew(sqlCmd);
            }        
            else MessageBox.Show("בדוק את השדות המודגשים", "שגיאות בפרטים", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            tCard.changeMainPanel();
        }

        private bool validateUserInput()
        {
            if (!PaintWithYellow() && checkDates())
            {
                //a popup alert when the Tahasuka is more than 30 days.
                if (dateTimePickerStart.Value.AddDays(30).CompareTo(dateTimePickerEnd.Value) < 0)
                {
                  if (MessageBox.Show("?האם אתה בטוח שמשך התעסוקה הוא יותר מ30- ימים", "ווידוא תאריכים",
                        MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk,
                        MessageBoxDefaultButton.Button1)
                        != DialogResult.OK)
                        return false;
                }
            }
            //checks if there's a row with this startDate in TahasukaData Table.
            SqlCommand sqlCmd = SqlManager.SqlCommandFactory(@"SELECT COUNT(*) FROM TahasukaData WHERE startDate=@startDate");
            sqlCmd.Parameters.Add("@startDate", SqlDbType.Date).Value = dateTimePickerStart.Value;
            if (SqlManager.executeSqlScalar(sqlCmd) == 0)
                return true;
            else MessageBox.Show(@".קיימת תעסוקה בתאריך ההתחלה המוזן. בחר תאריך התחלה אחר", "שגיאות בפרטים", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            return false;                                              
        }

    }
}
